﻿Imports System.IO
Imports System.Xml
Module LoadPlayer
    Private BackupUserData As String
    Public Function LoadPlayer(Optional ByVal PlayerNumber As Short = 0)
        'declare xml dimensions
        Dim m_xmld As XmlDocument
        Dim m_nodelist As XmlNodeList
        Dim m_node As XmlNode
        Dim CurNumber, MaxNumber As Integer
        Try
            m_xmld = New XmlDocument()
            m_xmld.Load(MainProgram.DirPlayer + MainProgram.CapitalizeEachWord(MainProgram.User_Name) + ".xml")
            m_nodelist = m_xmld.SelectNodes("/savefile")
            For Each m_node In m_nodelist
                MainProgram.User_Name = m_node.ChildNodes.Item(0).ChildNodes.Item(0).InnerText
                MainProgram.User_Password = m_node.ChildNodes.Item(1).ChildNodes.Item(0).InnerText
                MainProgram.UserLocation_EntranceMap = m_node.ChildNodes.Item(2).ChildNodes.Item(0).InnerText
                MainProgram.UserLocation_EntranceSector = m_node.ChildNodes.Item(3).ChildNodes.Item(0).InnerText
                MainProgram.User_Experience = Val(m_node.ChildNodes.Item(4).ChildNodes.Item(0).InnerText)
                MainProgram.User_PracticePoints = Val(m_node.ChildNodes.Item(5).ChildNodes.Item(0).InnerText)
                MainProgram.User_TrainingPoints = Val(m_node.ChildNodes.Item(6).ChildNodes.Item(0).InnerText)
                MainProgram.User_Strengthmod = Val(m_node.ChildNodes.Item(7).ChildNodes.Item(0).InnerText)
                MainProgram.User_Strength = Val(m_node.ChildNodes.Item(8).ChildNodes.Item(0).InnerText)
                MainProgram.User_StrengthMax = Val(m_node.ChildNodes.Item(9).ChildNodes.Item(0).InnerText)
                MainProgram.User_Dexteritymod = Val(m_node.ChildNodes.Item(10).ChildNodes.Item(0).InnerText)
                MainProgram.User_Dexterity = Val(m_node.ChildNodes.Item(11).ChildNodes.Item(0).InnerText)
                MainProgram.User_DexterityMax = Val(m_node.ChildNodes.Item(12).ChildNodes.Item(0).InnerText)
                MainProgram.User_Constitutionmod = Val(m_node.ChildNodes.Item(13).ChildNodes.Item(0).InnerText)
                MainProgram.User_Constitution = Val(m_node.ChildNodes.Item(14).ChildNodes.Item(0).InnerText)
                MainProgram.User_ConstitutionMax = Val(m_node.ChildNodes.Item(15).ChildNodes.Item(0).InnerText)
                MainProgram.User_Intelligencemod = Val(m_node.ChildNodes.Item(16).ChildNodes.Item(0).InnerText)
                MainProgram.User_Intelligence = Val(m_node.ChildNodes.Item(17).ChildNodes.Item(0).InnerText)
                MainProgram.User_IntelligenceMax = Val(m_node.ChildNodes.Item(18).ChildNodes.Item(0).InnerText)
                MainProgram.User_Wisdommod = Val(m_node.ChildNodes.Item(19).ChildNodes.Item(0).InnerText)
                MainProgram.User_Wisdom = Val(m_node.ChildNodes.Item(20).ChildNodes.Item(0).InnerText)
                MainProgram.User_WisdomMax = Val(m_node.ChildNodes.Item(21).ChildNodes.Item(0).InnerText)
                MainProgram.User_Charismamod = Val(m_node.ChildNodes.Item(22).ChildNodes.Item(0).InnerText)
                MainProgram.User_Charisma = Val(m_node.ChildNodes.Item(23).ChildNodes.Item(0).InnerText)
                MainProgram.User_CharismaMax = Val(m_node.ChildNodes.Item(24).ChildNodes.Item(0).InnerText)
                MainProgram.User_Luckmod = Val(m_node.ChildNodes.Item(25).ChildNodes.Item(0).InnerText)
                MainProgram.User_Luck = Val(m_node.ChildNodes.Item(26).ChildNodes.Item(0).InnerText)
                MainProgram.User_LuckMax = Val(m_node.ChildNodes.Item(27).ChildNodes.Item(0).InnerText)
                MainProgram.User_Vern = m_node.ChildNodes.Item(28).ChildNodes.Item(0).InnerText
                MainProgram.User_Dover = m_node.ChildNodes.Item(29).ChildNodes.Item(0).InnerText
                MainProgram.User_Sovern = m_node.ChildNodes.Item(30).ChildNodes.Item(0).InnerText
                MainProgram.User_BankVern = m_node.ChildNodes.Item(31).ChildNodes.Item(0).InnerText
                MainProgram.User_BankDover = m_node.ChildNodes.Item(32).ChildNodes.Item(0).InnerText
                MainProgram.User_BankSovern = m_node.ChildNodes.Item(33).ChildNodes.Item(0).InnerText
                MainProgram.User_CurHP = m_node.ChildNodes.Item(34).ChildNodes.Item(0).InnerText
                MainProgram.User_MaxHP = m_node.ChildNodes.Item(35).ChildNodes.Item(0).InnerText
                MainProgram.User_CurWP = m_node.ChildNodes.Item(36).ChildNodes.Item(0).InnerText
                MainProgram.User_MaxWP = m_node.ChildNodes.Item(37).ChildNodes.Item(0).InnerText
                MainProgram.User_CurSP = m_node.ChildNodes.Item(38).ChildNodes.Item(0).InnerText
                MainProgram.User_MaxSP = m_node.ChildNodes.Item(39).ChildNodes.Item(0).InnerText
                MainProgram.User_Class = m_node.ChildNodes.Item(40).ChildNodes.Item(0).InnerText
                MainProgram.User_Race = m_node.ChildNodes.Item(41).ChildNodes.Item(0).InnerText
                MainProgram.User_Age = m_node.ChildNodes.Item(42).ChildNodes.Item(0).InnerText
                MainProgram.User_Birthday = m_node.ChildNodes.Item(43).ChildNodes.Item(0).InnerText
                MainProgram.User_Gender = m_node.ChildNodes.Item(44).ChildNodes.Item(0).InnerText
                MainProgram.User_HomeProvince = m_node.ChildNodes.Item(45).ChildNodes.Item(0).InnerText
                MainProgram.User_Rode = m_node.ChildNodes.Item(46).ChildNodes.Item(0).InnerText
                MainProgram.User_Weight = m_node.ChildNodes.Item(47).ChildNodes.Item(0).InnerText
                MainProgram.User_Height = m_node.ChildNodes.Item(48).ChildNodes.Item(0).InnerText
                MainProgram.User_NaturalAC = Val(m_node.ChildNodes.Item(49).ChildNodes.Item(0).InnerText)
                MainProgram.User_Level = Val(m_node.ChildNodes.Item(50).ChildNodes.Item(0).InnerText)
                MainProgram.UserEquip_Mainhand = m_node.ChildNodes.Item(51).ChildNodes.Item(0).InnerText
                MainProgram.UserEquip_MainhandNUM = m_node.ChildNodes.Item(51).Attributes.GetNamedItem("number").Value
                If MainProgram.UserEquip_MainhandNUM <> "" And MainProgram.UserEquip_MainhandNUM <> "0" Then
                    MainProgram.AddArmorClass(MainProgram.UserEquip_MainhandNUM)
                End If
                MainProgram.UserEquip_Offhand = Replace(m_node.ChildNodes.Item(52).ChildNodes.Item(0).InnerText, "Nothing", "")
                MainProgram.UserEquip_OffhandNUM = m_node.ChildNodes.Item(52).Attributes.GetNamedItem("number").Value
                If MainProgram.UserEquip_OffhandNUM <> "" And MainProgram.UserEquip_OffhandNUM <> "0" Then
                    MainProgram.AddArmorClass(MainProgram.UserEquip_OffhandNUM)
                End If
                MainProgram.UserEquip_Range = Replace(m_node.ChildNodes.Item(53).ChildNodes.Item(0).InnerText, "Nothing", "")
                MainProgram.UserEquip_RangeNUM = m_node.ChildNodes.Item(53).Attributes.GetNamedItem("number").Value
                If MainProgram.UserEquip_Range <> "" And MainProgram.UserEquip_RangeNUM <> "0" Then
                    MainProgram.AddArmorClass(MainProgram.UserEquip_RangeNUM)
                End If
                MainProgram.UserEquip_Head = Replace(m_node.ChildNodes.Item(54).ChildNodes.Item(0).InnerText, "Nothing", "")
                MainProgram.UserEquip_HeadNUM = m_node.ChildNodes.Item(54).Attributes.GetNamedItem("number").Value
                If MainProgram.UserEquip_Head <> "" And MainProgram.UserEquip_HeadNUM <> "0" Then
                    MainProgram.AddArmorClass(MainProgram.UserEquip_HeadNUM)
                End If
                MainProgram.UserEquip_Neck = Replace(m_node.ChildNodes.Item(55).ChildNodes.Item(0).InnerText, "Nothing", "")
                MainProgram.UserEquip_NeckNUM = m_node.ChildNodes.Item(55).Attributes.GetNamedItem("number").Value
                If MainProgram.UserEquip_Neck <> "" And MainProgram.UserEquip_NeckNUM <> "0" Then
                    MainProgram.AddArmorClass(MainProgram.UserEquip_NeckNUM)
                End If
                MainProgram.UserEquip_Shoulders = Replace(m_node.ChildNodes.Item(56).ChildNodes.Item(0).InnerText, "Nothing", "")
                MainProgram.UserEquip_ShouldersNUM = m_node.ChildNodes.Item(56).Attributes.GetNamedItem("number").Value
                If MainProgram.UserEquip_Shoulders <> "" And MainProgram.UserEquip_ShouldersNUM <> "0" Then
                    MainProgram.AddArmorClass(MainProgram.UserEquip_ShouldersNUM)
                End If
                MainProgram.UserEquip_Chest = Replace(m_node.ChildNodes.Item(57).ChildNodes.Item(0).InnerText, "Nothing", "")
                MainProgram.UserEquip_ChestNUM = m_node.ChildNodes.Item(57).Attributes.GetNamedItem("number").Value
                If MainProgram.UserEquip_Chest <> "" And MainProgram.UserEquip_ChestNUM <> "0" Then
                    MainProgram.AddArmorClass(MainProgram.UserEquip_ChestNUM)
                End If
                MainProgram.UserEquip_Back = Replace(m_node.ChildNodes.Item(58).ChildNodes.Item(0).InnerText, "Nothing", "")
                MainProgram.UserEquip_BackNUM = m_node.ChildNodes.Item(59).Attributes.GetNamedItem("number").Value
                If MainProgram.UserEquip_Back <> "" And MainProgram.UserEquip_BackNUM <> "0" Then
                    MainProgram.AddArmorClass(MainProgram.UserEquip_BackNUM)
                End If
                MainProgram.UserEquip_Arms = Replace(m_node.ChildNodes.Item(59).ChildNodes.Item(0).InnerText, "Nothing", "")
                MainProgram.UserEquip_ArmsNUM = m_node.ChildNodes.Item(59).Attributes.GetNamedItem("number").Value
                If MainProgram.UserEquip_Arms <> "" And MainProgram.UserEquip_ArmsNUM <> "0" Then
                    MainProgram.AddArmorClass(MainProgram.UserEquip_ArmsNUM)
                End If
                MainProgram.UserEquip_Hands = Replace(m_node.ChildNodes.Item(60).ChildNodes.Item(0).InnerText, "Nothing", "")
                MainProgram.UserEquip_HandsNUM = m_node.ChildNodes.Item(60).Attributes.GetNamedItem("number").Value
                If MainProgram.UserEquip_Hands <> "" And MainProgram.UserEquip_HandsNUM <> "0" Then
                    MainProgram.AddArmorClass(MainProgram.UserEquip_HandsNUM)
                End If
                MainProgram.UserEquip_Wrists = Replace(m_node.ChildNodes.Item(61).ChildNodes.Item(0).InnerText, "Nothing", "")
                MainProgram.UserEquip_WristsNUM = m_node.ChildNodes.Item(61).Attributes.GetNamedItem("number").Value
                If MainProgram.UserEquip_Wrists <> "" And MainProgram.UserEquip_WristsNUM <> "0" Then
                    MainProgram.AddArmorClass(MainProgram.UserEquip_WristsNUM)
                End If
                MainProgram.UserEquip_LFinger = Replace(m_node.ChildNodes.Item(62).ChildNodes.Item(0).InnerText, "Nothing", "")
                MainProgram.UserEquip_LFingerNUM = m_node.ChildNodes.Item(62).Attributes.GetNamedItem("number").Value
                If MainProgram.UserEquip_LFinger <> "" And MainProgram.UserEquip_LFingerNUM <> "0" Then
                    MainProgram.AddArmorClass(MainProgram.UserEquip_LFingerNUM)
                End If
                MainProgram.UserEquip_RFinger = Replace(m_node.ChildNodes.Item(63).ChildNodes.Item(0).InnerText, "Nothing", "")
                MainProgram.UserEquip_RFingerNUM = m_node.ChildNodes.Item(63).Attributes.GetNamedItem("number").Value
                If MainProgram.UserEquip_RFinger <> "" And MainProgram.UserEquip_RFingerNUM <> "0" Then
                    MainProgram.AddArmorClass(MainProgram.UserEquip_RFingerNUM)
                End If
                MainProgram.UserEquip_Waist = Replace(m_node.ChildNodes.Item(64).ChildNodes.Item(0).InnerText, "Nothing", "")
                MainProgram.UserEquip_WaistNUM = m_node.ChildNodes.Item(64).Attributes.GetNamedItem("number").Value
                If MainProgram.UserEquip_Waist <> "" And MainProgram.UserEquip_WaistNUM <> "0" Then
                    MainProgram.AddArmorClass(MainProgram.UserEquip_WaistNUM)
                End If
                MainProgram.UserEquip_Legs = Replace(m_node.ChildNodes.Item(65).ChildNodes.Item(0).InnerText, "Nothing", "")
                MainProgram.UserEquip_LegsNUM = m_node.ChildNodes.Item(65).Attributes.GetNamedItem("number").Value
                If MainProgram.UserEquip_Legs <> "" And MainProgram.UserEquip_LegsNUM <> "0" Then
                    MainProgram.AddArmorClass(MainProgram.UserEquip_LegsNUM)
                End If
                MainProgram.UserEquip_Boots = Replace(m_node.ChildNodes.Item(66).ChildNodes.Item(0).InnerText, "Nothing", "")
                MainProgram.UserEquip_BootsNUM = m_node.ChildNodes.Item(66).Attributes.GetNamedItem("number").Value
                If MainProgram.UserEquip_Boots <> "" And MainProgram.UserEquip_BootsNUM <> "0" Then
                    MainProgram.AddArmorClass(MainProgram.UserEquip_BootsNUM)
                End If
                MainProgram.User_Citizenship = m_node.ChildNodes.Item(67).ChildNodes.Item(0).InnerText
                MainProgram.User_Clan = m_node.ChildNodes.Item(68).ChildNodes.Item(0).InnerText
                MainProgram.User_ClanRank = m_node.ChildNodes.Item(69).ChildNodes.Item(0).InnerText
                MainProgram.User_Tradesguild = m_node.ChildNodes.Item(70).ChildNodes.Item(0).InnerText
                MainProgram.User_TradesguildRank = m_node.ChildNodes.Item(71).ChildNodes.Item(0).InnerText
                MainProgram.User_Loyalty = m_node.ChildNodes.Item(72).ChildNodes.Item(0).InnerText
                MainProgram.User_Language_Common = Val(m_node.ChildNodes.Item(73).ChildNodes.Item(0).InnerText)
                MainProgram.User_Language_Narsik = Val(m_node.ChildNodes.Item(74).ChildNodes.Item(0).InnerText)
                MainProgram.User_Language_Northern = Val(m_node.ChildNodes.Item(75).ChildNodes.Item(0).InnerText)
                MainProgram.User_Language_Draconian = Val(m_node.ChildNodes.Item(76).ChildNodes.Item(0).InnerText)
                MainProgram.User_Language_Celestial = Val(m_node.ChildNodes.Item(77).ChildNodes.Item(0).InnerText)
                MainProgram.User_Language_Infernal = Val(m_node.ChildNodes.Item(78).ChildNodes.Item(0).InnerText)
                MainProgram.User_Language_Arcanic = Val(m_node.ChildNodes.Item(79).ChildNodes.Item(0).InnerText)
                MainProgram.User_Resistance_Fire = Val(m_node.ChildNodes.Item(80).ChildNodes.Item(0).InnerText)
                MainProgram.User_Resistance_Water = Val(m_node.ChildNodes.Item(81).ChildNodes.Item(0).InnerText)
                MainProgram.User_Resistance_Air = Val(m_node.ChildNodes.Item(82).ChildNodes.Item(0).InnerText)
                MainProgram.User_Resistance_Earth = Val(m_node.ChildNodes.Item(83).ChildNodes.Item(0).InnerText)
                MainProgram.User_Resistance_Spirit = Val(m_node.ChildNodes.Item(84).ChildNodes.Item(0).InnerText)
                MainProgram.User_Amplification_Fire = Val(m_node.ChildNodes.Item(85).ChildNodes.Item(0).InnerText)
                MainProgram.User_Amplification_Water = Val(m_node.ChildNodes.Item(86).ChildNodes.Item(0).InnerText)
                MainProgram.User_Amplification_Air = Val(m_node.ChildNodes.Item(87).ChildNodes.Item(0).InnerText)
                MainProgram.User_Amplification_Earth = Val(m_node.ChildNodes.Item(88).ChildNodes.Item(0).InnerText)
                MainProgram.User_Amplification_Spirit = Val(m_node.ChildNodes.Item(89).ChildNodes.Item(0).InnerText)
                MainProgram.User_SubResistance_Fire = Val(m_node.ChildNodes.Item(90).ChildNodes.Item(0).InnerText)
                MainProgram.User_SubResistance_Water = Val(m_node.ChildNodes.Item(91).ChildNodes.Item(0).InnerText)
                MainProgram.User_SubResistance_Air = Val(m_node.ChildNodes.Item(92).ChildNodes.Item(0).InnerText)
                MainProgram.User_SubResistance_Earth = Val(m_node.ChildNodes.Item(93).ChildNodes.Item(0).InnerText)
                MainProgram.User_SubResistance_Spirit = Val(m_node.ChildNodes.Item(94).ChildNodes.Item(0).InnerText)
                MainProgram.User_SubAmplification_Fire = Val(m_node.ChildNodes.Item(95).ChildNodes.Item(0).InnerText)
                MainProgram.User_SubAmplification_Water = Val(m_node.ChildNodes.Item(96).ChildNodes.Item(0).InnerText)
                MainProgram.User_SubAmplification_Air = Val(m_node.ChildNodes.Item(97).ChildNodes.Item(0).InnerText)
                MainProgram.User_SubAmplification_Earth = Val(m_node.ChildNodes.Item(98).ChildNodes.Item(0).InnerText)
                MainProgram.User_SubAmplification_Spirit = Val(m_node.ChildNodes.Item(99).ChildNodes.Item(0).InnerText)
                MaxNumber = Val(m_node.ChildNodes.Item(100).Attributes.GetNamedItem("number").Value)
                If MaxNumber > 0 Then 'there are items in the inventory
                    For CurNumber = 0 To MaxNumber Step 1
                        MainProgram.User_InventoryNUM(CurNumber) = m_node.ChildNodes.Item(100).ChildNodes.Item(CurNumber).Attributes.GetNamedItem("number").Value
                        MainProgram.User_Inventory(CurNumber) = m_node.ChildNodes.Item(100).ChildNodes.Item(CurNumber).InnerText
                    Next
                End If
                MaxNumber = Val(m_node.ChildNodes.Item(101).Attributes.GetNamedItem("number").Value)
                If MaxNumber > 0 Then 'there are abilities
                    For CurNumber = 0 To MaxNumber - 1 Step 1
                        MainProgram.User_Proficiency(CurNumber) = m_node.ChildNodes.Item(101).ChildNodes.Item(CurNumber).Attributes.GetNamedItem("name").Value
                        MainProgram.User_ProficiencyDesc(CurNumber) = m_node.ChildNodes.Item(101).ChildNodes.Item(CurNumber).InnerText
                    Next
                End If
                MaxNumber = Val(m_node.ChildNodes.Item(102).Attributes.GetNamedItem("number").Value)
                If MaxNumber > 0 Then 'there are abilities
                    For CurNumber = 0 To MaxNumber - 1 Step 1
                        MainProgram.AbilityName(CurNumber) = m_node.ChildNodes.Item(102).ChildNodes.Item(CurNumber).Attributes.GetNamedItem("name").Value
                        MainProgram.AbilityLevel(CurNumber) = m_node.ChildNodes.Item(102).ChildNodes.Item(CurNumber).Attributes.GetNamedItem("level").Value
                        MainProgram.AbilityLearned(CurNumber) = m_node.ChildNodes.Item(102).ChildNodes.Item(CurNumber).InnerText
                    Next
                End If
            Next
            Return 1
        Catch errorVariable As Exception
            MainProgram.SND("/RERROR: " + errorVariable.ToString())
            Return 0
        End Try
    End Function
    Public Sub AssignClassVariables(ByVal ClassLibrary As String)
        Dim IgnoreLine, ProficiencyLine, AbilityLine As Boolean
        Dim StepNumber As Integer = 0 'Recursion number used for ability and proficiency
        Dim StartPosition As Integer = 1
        Dim Steps As Integer
        For Steps = 1 To Len(ClassLibrary) Step 1
            Dim test As String
            Dim test2 As String = Mid(ClassLibrary, Steps, 1)
            Dim test3 As Integer = Asc(Mid(ClassLibrary, Steps, 1))
            If Mid(ClassLibrary, Steps, 1) = "|" Then
                StepNumber += 1
                StartPosition = Steps + 1
            ElseIf Mid(ClassLibrary, Steps, 1) = "[" Then
                If IgnoreLine = True Then 'Class line not currently used
                ElseIf ProficiencyLine = True Then 'proficiency line name
                    test = Mid(ClassLibrary, StartPosition, Steps - StartPosition)
                    MainProgram.User_Proficiency(StepNumber) = Mid(ClassLibrary, StartPosition, Steps - StartPosition)
                ElseIf AbilityLine = True Then 'ability line name
                    test = Mid(ClassLibrary, StartPosition, Steps - StartPosition)
                    MainProgram.AbilityName(StepNumber) = Mid(ClassLibrary, StartPosition, Steps - StartPosition)
                End If
                StartPosition = Steps + 1
            ElseIf Mid(ClassLibrary, Steps, 1) = "]" Then
                If IgnoreLine = True Then 'class line not currently used
                ElseIf ProficiencyLine = True Then 'proficiency line description
                    test = Mid(ClassLibrary, StartPosition, Steps - StartPosition)
                    MainProgram.User_ProficiencyDesc(StepNumber) = Mid(ClassLibrary, StartPosition, Steps - StartPosition)
                ElseIf AbilityLine = True Then 'ability line level
                    test = Mid(ClassLibrary, StartPosition, Steps - StartPosition)
                    MainProgram.AbilityLevel(StepNumber) = Mid(ClassLibrary, StartPosition, Steps - StartPosition)
                End If
            ElseIf Mid(ClassLibrary, Steps, 1) = "<" Then
                IgnoreLine = True
            ElseIf Mid(ClassLibrary, Steps, 1) = Chr(13) Or Mid(ClassLibrary, Steps, 1) = Chr(11) Then
                'this takes both carriage returns as it supports the old
                'version of richtext that was used during alpha stage of PloS
                If IgnoreLine = True Then 'not currently used
                    IgnoreLine = False
                    ProficiencyLine = True
                    StepNumber = 0 'reset back to zero for next lines use
                ElseIf ProficiencyLine = True Then 'second line is proficiency line
                    ProficiencyLine = False
                    AbilityLine = True
                    StepNumber = 0 'reset back to zero for next lines use
                ElseIf AbilityLine = True Then 'third line and last line is ability line
                    Exit For
                End If
                StartPosition = Steps + 1 'can't have stuff from the last line
            End If
        Next
    End Sub
End Module
